﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ISUM_V2
{
    public class Debugger
    {
        private StreamWriter _logwriter = null;
        private static bool _isEnable = true;
        private object dataLock = new object();
        private static readonly Debugger oDebugger = new Debugger();

        public Debugger ()
        {
            Initialization();
        }
        ~Debugger ()
        {

        }
        private void Initialization ()
        {
            try
            {
                string logFilePath = string.Format( "{0}/log/APPLOG_{1}.log", AppDomain.CurrentDomain.BaseDirectory,
                    DateTime.Now.ToString( "yyyy_MM_dd" ) );
                if ( !Directory.Exists( Path.GetDirectoryName( @logFilePath ) ) )
                {
                    Directory.CreateDirectory( Path.GetDirectoryName( logFilePath ) );
                }
                lock ( dataLock )
                {
                    //Open up the streamwriter for writing...
                    _logwriter = File.AppendText( logFilePath );
                }
            }
            catch ( Exception ) { }
        }
        public static Debugger GetInstance ( bool isEnable )
        {
            _isEnable = isEnable;
            return oDebugger;
        }
        public static Debugger GetInstance ()
        {
            return oDebugger;
        }
        public void PrintLog ( string prefix, string message )
        {
            try
            {
                if ( prefix == null )
                    prefix = "ApplicationName";
                if ( _isEnable )
                {
                    Console.WriteLine( string.Format( "{0} :: [{1}]\t{2}", DateTime.Now.ToString( "HH:mm ss tt" ), prefix, message ) );
                    _logwriter.WriteLine( string.Format( "{0} :: [{1}]\t{2}", DateTime.Now.ToString( "HH:mm ss tt" ), prefix, message ) );
                    _logwriter.Flush();
                }
            }
            catch ( Exception ) { }
        }
    }
}
